package cn.tedu.v5;

import java.io.*;
import java.net.Socket;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Scanner;

/**
 *  Version 4客户端 登录  退出：退两次
 */
public class Client {
    private static final String HOST = "172.20.10.3";
    private static final Integer PORT = 1019;
    private Socket socket;
    private String ip;
    private PrintWriter pw;
    private BufferedReader br;


    public Client() {
        try {
            socket = new Socket(HOST, PORT);
            ip = socket.getInetAddress().getHostAddress();
            br = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF8"));
            pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8")), true);
        } catch (SocketException e) {
            System.out.println("连接被重置或者断开");
        } catch (UnknownHostException e) {
            System.out.println("IP地址有问题，请询问服务器端！");
        } catch (IOException e) {
            System.out.println("客户端主动退出了：断开链接");
        }
    }


    public void start() {
        System.out.println("----欢迎来到YU牌QQ聊天室----");
        Scanner input=new Scanner(System.in);
        //目的：不捕获，确定没有问题，捕获代码块具体针对服务器结构
        try {
            /*一、接受登陆提示
             * 1.读入服务器端---输出流
             */
            String serverMessage = br.readLine();//服务器端：写“请输入用户名”
            System.out.println(serverMessage);//读入账号，必须写出
            //2.写出用户名
            String uername=input.nextLine();
            //3.写出用户名发给服务器
            pw.println(uername);//写出给服务器端--获取账号

             //二、接受登陆密码提示
            serverMessage = br.readLine();//服务器端：写“请输入密码”
            System.out.println(serverMessage);//打印：请输入密码
            //2.写出密码
            String password=input.nextLine();
            //3.写出密码发给服务器
            pw.println(password);//写出给服务器端--获取密码

            //三、服务器端验证账号跟密码是否正确
            //3.1读入账号和密码正确的情况
            serverMessage=br.readLine();//登录成功，欢迎进入Yu牌聊天室
            System.out.println(serverMessage);//客户端打印：登陆成功
            //3.2判断如果是“登录成功，欢迎进入Yu牌聊天室”前缀：登录成功
            if (serverMessage.startsWith("登录成功")) {//true 聊天
                System.out.println("请客户端输入");
                chat();//聊天功能

            }
        }catch (IOException e){
        } finally {
            if (pw != null)
                pw.close();
            if (br != null)
                try {
                    br.close();
                } catch (IOException e) {
                    System.out.println("服务器通讯内容失败~~");
                }
            if (socket != null && !socket.isClosed()) {
                System.out.println("客户端关闭成功！success~");
                try {
                    socket.close();
                } catch (IOException e) {
                    System.out.println("客户端关闭失败");
                }
            }
        }
    }

    //1.放在Start方法下---封装 客户端 相互通话方法  chat
    private void chat() {
        new Thread(() -> {
            try {
                String message;
                while ((message = br.readLine()) != null) {
                    System.out.println("服务器说：" + message);
                    System.out.print("客户端请输入：");
                }
            } catch (IOException e) {
                System.out.println("服务器端发来的消息断开了~");
            }
        }).start();

        Scanner input = new Scanner(System.in);
        while (true) {
            System.out.println("客户端请输入：");
            String sme = input.nextLine();
            pw.println(ip + "客户端说：" + sme);
        }
    }

    public static void main(String[] args) {
        new Client().start();

    }
}

